/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forms;

import com.sun.lwuit.Command;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import entidade.Categoria;
import entidade.MobCatController;
import exceptions.ReturnWsException;
import form.components.CategoriaListRenderer;
import java.util.Vector;

/**
 *
 * @author ademar.victorino
 */
public class CategoriaForm extends AbstractForm {

    private Categoria catCorrente = null;
    private List list = null;
    private CategoriasRaizForm returnForm;

    public CategoriaForm() {

        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

    }

    public void show(Categoria catCorrente, CategoriasRaizForm returnForm) {
        this.catCorrente = catCorrente;
        init();
        super.show();
    }

    public void show() {
        super.show();
        if (list != null) {
            System.out.println(list.hasFocus());
            list.setFocusable(true);
            list.setFocus(true);
            list.setFocusPainted(true);
            setFocused(list);
            list.requestFocus();
        }
    }

    private void init() {
        setTitle(catCorrente.nome);
        if (list != null) {
            this.removeComponent(list);
        }
        try {
            list = new List(/*sortByLabelAlpha(*/MobCatController.getInstance().listSubCategoria(catCorrente.id)/*)*/);
        } catch (ReturnWsException ex) {
            ex.showError(returnForm);
        }
        addComponent(list);
        list.setListCellRenderer(new CategoriaListRenderer());

        addCommand(new Command("Voltar") {

            public void actionPerformed(ActionEvent evt) {

                voltarAction();

            }
        });

        list.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                WaitForm.getInstance().show();
                new Thread(new Runnable() {

                    public void run() {
                        goToNextForm();
                    }
                }).start();
            }
        });
    }

    public void voltarAction() {

        if (catCorrente.paiId == -1) {
            if (returnForm != null) {
                returnForm.show();
            } else {
                new CategoriasRaizForm().show();
            }
        } else {
            try {
                catCorrente = MobCatController.getInstance().getCategoria(catCorrente.paiId);
            } catch (ReturnWsException ex) {
                ex.showError(returnForm);
            }
            init();
        //this.repaint();
        }

    }

    public void goToNextForm() {
        Categoria catSel = (Categoria) list.getSelectedItem();
        if (catSel.possuiCategoriaFilha) {
            this.catCorrente = catSel;
            init();
            WaitForm.getInstance().setVisible(false);
            this.show();
        //show();
        } else {
            new RegistrosForm(this).show(catSel);
        }
    }

    public Vector sortByLabelAlpha(Vector elements) {
        Vector returnVal = new Vector(elements.size());
        for (int a = 0; a < elements.size(); a++) {
            returnVal.addElement(new Categoria());
        }
        final int vectorSize = elements.size();

        for (int i = 1; i < vectorSize; i++) {
            final Categoria mObj = (Categoria) elements.elementAt(i);
            int j = i - 1;
            for (; j >= 0; j--) {
                Categoria r = (Categoria) elements.elementAt(j);
                if (r.nome.compareTo(mObj.nome) <= 0) {
                    break;
                }
                returnVal.removeElementAt(j + 1);
                returnVal.insertElementAt(r, j + 1);
            }
            returnVal.removeElementAt(j + 1);
            returnVal.insertElementAt(mObj, j + 1);
        }
        return returnVal;
    }
}
